﻿#include "SeqList.h"

void InitSeqList(SL* ps)
{
	ps->a = (SQDataType*)malloc(sizeof(SQDataType)*2);
	ps->capacity = 2;
	ps->size = 0;
}

void printSql(SL* s)
{
	int i = 0;
	for (i = 0; i <= s->size - 1; i++)
	{
		printf("%d ", s->a[i]);
	}
	printf("\n");
}

void checkExpandCapacity(SL* s)
	{
		if (s->size == s->capacity)
		{
			SQDataType* b =(SQDataType*)realloc(s->a, sizeof(SQDataType) * s->capacity * 2);
			if (b == NULL)
			{
				printf("分配内存失败\n");
				exit(-1);
			}
			else
			{
				s->a = b;
				s->capacity *= 2;
			}
		}

	}


void SeqListPushBack(SL* ps, SQDataType x)
{
	checkExpandCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}
void SeqListPopBack(SL* ps)
{
	assert(ps->size > 0);
	ps->size--;
}

void SeqListPopFront(SL* ps)
{
	assert(ps->size > 0);
	int start = 1;
	while (start < ps->size)
	{
		ps->a[start - 1] = ps->a[start];
		start++;
	}
	ps->size--;
}

void SeqListPushFront(SL* ps, SQDataType x)
{
	checkExpandCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}


void SeqListInsert(SL* ps, SQDataType x, int pos)
{
	assert(pos < ps->size);
	checkExpandCapacity(&ps);
	int end = ps->size-1;
	while (end>=pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SL* ps, int pos)
{
	assert(pos < ps->size);
	int start = pos + 1;
	while (start<ps->size)
	{
		ps->a[start - 1] = ps->a[start];
		start++;
	}
	ps->size--;
}